/**
 * 
 */
"use strict";

var traintest = traintest || {};

traintest.ground = null;

/*
 * initialize request anim. frame
 */
window.requestAnimationFrame = window.requestAnimationFrame
	|| window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame
	|| window.oRequestAnimationFrame;

/*
 * load images method
 */
traintest.loadImages = function(images, callback) {
	var imagesLoaded = 0;
	$.each(images, function(i, filename) {
		var img = new Image();
		img.onload = function() {
			if (++imagesLoaded === images.length)
				callback.call();
		};
		img.src = 'img/' + filename;
	});
};

traintest.getColumns = function() {
	return Math.floor(traintest.ground.width() / defs.FIELD_WIDTH);
};

traintest.getRows = function() {
	return Math.floor(traintest.ground.height() / defs.FIELD_HEIGHT);
};

traintest.init = function(restore) {
	var GROUND_COLUMNS, GROUND_ROWS;
	traintest.ground = $('#ground');
	GROUND_COLUMNS = traintest.getColumns();
	GROUND_ROWS = traintest.getRows();

	// initialize fields
	fields.init(traintest.ground, GROUND_COLUMNS, GROUND_ROWS);

	if (restore) {
		// restore
		traintest.restore();
	}

	// tool bar
	toolbar.init();

	// start clock
	traintest.clock = new utils.Clock();

	// start update
	window.requestAnimationFrame(traintest.update);

	// on close event
	$(window).on("beforeunload", function() {
		if (confirm("Do you really want to close?")) {
			traintest.save();
			return true;
		} else
			return false;
	});

};

/*
 * start initialization when document loaded
 */
$(document)
	.ready(function(e) {

		var images = [ 'fields_collection.png', 'movable_collection.png',
			'rail_collection.png', 'steam.png', 'explosion_1.png',
			'explosion_2.png', 'explosion_3.png', 'explosion_4.png',
			'explosion_5.png', 'explosion_6.png', 'explosion_7.png',
			'rail_signal_off.png', 'rail_signal_green.png',
			'rail_signal_red.png', 'rail_switch_toggle_request.png',
			'rail_switch_green_request.png', 'rail_switch_red_request.png' ];

		traintest
			.loadImages(images, function() {

				/**
				 * Start initialization
				 */
				traintest.init(true);

				traintest.groundPosition = new fields.FieldPosition(0, 0);

				/*
				 * MOUSE MOVE
				 */
				traintest.ground.mousemove(function(e) {
					if (!toolbar.applier)
						return;
					var left, top;
					// calculate on element position
					left = e.pageX - traintest.ground.position().left;
					top = e.pageY - traintest.ground.position().top;
					// get old position
					var absPos = traintest.groundPosition.getLeftTop();
					// update old position
					traintest.groundPosition.setPosCss(left, top);
					// update left/top position on change
					if (absPos != traintest.groundPosition.getLeftTop()) {
						absPos = traintest.groundPosition.getLeftTop();
						toolbar.applier.css({
							left : absPos.left,
							top : absPos.top
						});
					}
				});

				/*
				 * GROUND CLICK
				 */
				$(traintest.ground).on('click', function(e) {
					console.log('Clicked the ground');
				});

				/*
				 * COLLISIONS
				 */
				$(traintest.ground)
					.on('collision', function(event, field1, field2, distance, velocity) {
						console.log('Collision detected between ' + field1
							+ ' and ' + field2 + ', with center distance of '
							+ distance + ' and collision speed of ' + velocity);
						if (velocity > 0.1) {
							if (field1 instanceof movable.Movable) {
								if (field1.motion)
									field1.motion.interrupt();
							}
							if (field2 instanceof movable.Movable) {
								if (field2.motion)
									field2.motion.interrupt();
							}
						}
						if (velocity > 0.3) {
							// destroy both
							var anim1 = new animation.explosion(field1, field1.vec2);
							anim1.place();
							var anim2 = new animation.explosion(field2, field2.vec2);
							anim2.place();
							field1.remove();
							field2.remove();

						}
					});

			});

	});
